package cn.schoolwow.workflow.module.task.action.flow.common;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import com.alibaba.fastjson.JSONObject;

public class ReplaceAssignerCommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        checkAssigner(flowContext);
        setByDefaultAssigner(flowContext);
        setByContextData(flowContext);
    }

    @Override
    public String name() {
        return "替换分配人变量";
    }

    private void checkAssigner(FlowContext flowContext){
        String assigner = flowContext.checkData("assigner", String.class);

        if(!assigner.startsWith("{{")&&!assigner.endsWith("}}")){
            flowContext.putReturnData("assigner", assigner);
            flowContext.brokenCurrentFlow("分配人为常量");
        }
        String assignerName = assigner.substring(2,assigner.length()-2);
        flowContext.putCurrentFlowData("assignerName", assignerName);
    }

    private void setByDefaultAssigner(FlowContext flowContext){
        String assignerName = flowContext.checkData("assignerName", String.class);

        switch (assignerName){
            case "内置变量.发起人":{
                WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
                flowContext.putReturnData("assigner", workFlowInstance.getInitiator());
                flowContext.brokenCurrentFlow("分配人为内部变量");
            }break;
        }
    }

    private void setByContextData(FlowContext flowContext){
        String assignerName = flowContext.checkData("assignerName", String.class);

        JSONObject contextData = null;
        Long workFlowInstanceId = null;
        if(flowContext.containKey("workFlowInstanceId")){
            DAO dao = flowContext.checkInstanceData(DAO.class);
            workFlowInstanceId = flowContext.checkData("workFlowInstanceId", Long.class);
            contextData = dao.query(WorkFlowInstance.class)
                    .addQuery("id", workFlowInstanceId)
                    .addColumn("contextData")
                    .execute()
                    .getSingleColumn(JSONObject.class);
        }else if(flowContext.containInstanceKey(WorkFlowInstance.class)){
            WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
            workFlowInstanceId = workFlowInstance.getId();
            contextData = workFlowInstance.getContextData();
        }
        if(null==contextData){
            throw new IllegalArgumentException("实例上下文数据获取失败!");
        }

        if(!contextData.containsKey(assignerName)){
            throw new IllegalArgumentException("任务分配人不存在!分配人:"+assignerName+",工作流实例id:"+workFlowInstanceId);
        }
        String assigner = contextData.getString(assignerName);
        if(null==assigner||assigner.isEmpty()){
            throw new IllegalArgumentException("任务分配人为空!分配人:"+assigner+",工作流实例id:"+workFlowInstanceId);
        }

        flowContext.putReturnData("assigner", assigner);
    }
}
